package com.ruoyi.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.teacher.domain.TChapter;
import com.ruoyi.teacher.domain.TOption;
import com.ruoyi.teacher.domain.TQuestion;
import com.ruoyi.teacher.domain.TQuestionBank;
import com.ruoyi.teacher.mapper.TChapterMapper;
import com.ruoyi.teacher.mapper.TOptionMapper;
import com.ruoyi.teacher.mapper.TQuestionBankMapper;
import com.ruoyi.teacher.vo.TQuestionVo;
import com.ruoyi.teacher.mapper.TQuestionMapper;
import com.ruoyi.teacher.service.ITQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 问题Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-03
 */
@Service
public class TQuestionServiceImpl extends ServiceImpl<TQuestionMapper, TQuestion> implements ITQuestionService {

    @Autowired
    TQuestionBankMapper tQuestionBankMapper;

    @Autowired
    TChapterMapper tChapterMapper;
    @Override
    public TableDataInfo<TQuestionVo> queryList(TQuestionVo entity) {
        // 分页查询题目数据
        Page<TQuestionVo> tQuestionVoPage = this.baseMapper.queryList(PageUtils.buildPage(), entity);
        List<TQuestionVo> records = tQuestionVoPage.getRecords();

        for (TQuestionVo record : records) {
            // 查询题库名称
            String bname = null;
            LambdaQueryWrapper<TQuestionBank> bqw = new LambdaQueryWrapper<>();
            bqw.eq(TQuestionBank::getId, record.getbId());
            TQuestionBank tQuestionBank = tQuestionBankMapper.selectOne(bqw);
            if (tQuestionBank != null) {
                bname = tQuestionBank.getName();
            }

            // 查询章节名称
            String cpname = null;
            LambdaQueryWrapper<TChapter> cqw = new LambdaQueryWrapper<>();
            cqw.eq(TChapter::getId, record.getCpId());
            TChapter tChapter = tChapterMapper.selectOne(cqw);
            if (tChapter != null) {
                cpname = tChapter.getName();
            }

//            查询选项
            TOption[] options = null;
            QueryWrapper<TOption> oqw = new QueryWrapper<>();
//            oqw.eq(TOption::getqId,record.getCpId())
            oqw.eq("q_id",record.getId());
            options = optionMapper.selectList(oqw).toArray(new TOption[0]);
            System.err.println(options);
            // 设置名称
            record.setOption(options);
            record.setBankName(bname);
            record.setChapterName(cpname);
        }

        return PageUtils.buildDataInfo(tQuestionVoPage);
    }

    @Override
    public List<TQuestionVo> queryAll(TQuestionVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public TQuestionVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }
    @Autowired
    TOptionMapper optionMapper;
    @Override
    @Transactional
    public boolean saveQuestionAndAnswer(TQuestionVo questionVo) {
//        获取题目
        TQuestion question = questionVo.getQuestion();
//        获取选项
        TOption[] options = questionVo.getOption();
//        获取题目类型
        String type = question.getType();
        if(baseMapper.queryById(question.getId())!=null){
//            如果题目存在，则更新题目
            baseMapper.updateById(question);
//            删除该题目的所有选项
            QueryWrapper<TOption> oqw = new QueryWrapper<>();
            oqw.eq("q_id",question.getId());
            optionMapper.delete(oqw);
        }else{
//            否则新增题目
            System.err.println(question);
            baseMapper.insert(question);
        }
//        如果题目是单选题或者多选题
        if("单选题".equals(type) || "多选题".equals(type)){
            StringBuilder str = new StringBuilder();
            for (TOption cur : options) {
//                给选项增加问题id
                cur.setqId(question.getId());
//                给选项增加cpid
                cur.setCpId(question.getCpId());
//                增加选项
                optionMapper.insert(cur);
                if (cur.getIsAnswer() == 1){
//                    是答案，记录id
                    Long curId = cur.getId();
                    if(str.length() == 0){
                        str.append(curId);
                    }else{
                        str.append(',').append(curId);
                    }
                }
            }
//            更新问题的答案
            question.setAnswer(str.toString());
            baseMapper.updateById(question);
        }
        return true;
    }



}
